package net.k3rnel.abugadro.util;

import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import net.k3rnel.abugadro.gcodes.CommentsFeedHandler;
import net.k3rnel.abugadro.gcodes.IssueFeedHandler;

import android.content.ContentResolver;
import android.util.Log;

public class SaxFeedParser {

	private ContentResolver content;
	private String feedUrl;
	private int projId;
	private int issueId;
	private int type;
	
    public SaxFeedParser(ContentResolver cr, String feedUrl, int projId, int issueId, int type){
    	content = cr;
    	this.feedUrl = feedUrl;
    	this.projId = projId;
    	this.issueId = issueId;
    	this.type = type;
    }
    
    public void parse() {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        try {
        	URL url = new URL(feedUrl);
            SAXParser parser = factory.newSAXParser();
            switch(type){
            case 0:
            	IssueFeedHandler issueHandler = new IssueFeedHandler(content, projId);
            	parser.parse(url.openConnection().getInputStream(), issueHandler);
            	break;
            case 1:
            	CommentsFeedHandler commentsHandler = new CommentsFeedHandler(content,projId,issueId);
            	parser.parse(url.openConnection().getInputStream(), commentsHandler);
            	break;
            default:
            	break;
            }
        } catch (Exception e) {
        	Log.e("Abugadro","Error",e);
            throw new RuntimeException(e);
        } 
    }
}
